package cn.kgc.ubagnet.controller;


import cn.kgc.ubagnet.pojo.Buyer;

import cn.kgc.ubagnet.service.buyer.BuyerService;
import cn.kgc.ubagnet.service.buyer.CrowdBuyerDetailService;
import cn.kgc.ubagnet.service.coupon.CouponService;
import cn.kgc.ubagnet.utils.Aliyun;
//import org.omg.CORBA.MARSHAL;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @author shkstart
 * @create 2020-08-04 11:37
 */
@Controller
public class BuyerController {

    @Resource
    BCryptPasswordEncoder bCryptPasswordEncoder;
    @Resource
    BuyerService buyerService;

   @RequestMapping("/regist/buyer")
   @ResponseBody
   public Map<String,Object> saveAdmin(String phone, String password) {
       Buyer buyer=new Buyer();
       buyer.setPhone(phone);
       buyer.setHeadimage("images/icons/logo.jpg");
       String encode = bCryptPasswordEncoder.encode(password);
       buyer.setPassword(encode);
       Integer integer = buyerService.addBuyer(buyer);
       //注册成功跳转到登录页面
       Map<String,Object> map=new HashMap<>();
       if(integer>0){
           map.put("msg","SUCCESS");
       }else{
           map.put("msg","FAIL");
       }
       return map;
   }


   //根据用户名查找用户
    @RequestMapping("/seleByPhone")
    @ResponseBody
    public Map<String,Object> seleByPhone(String phone){
        Buyer buyer = buyerService.seleByPhone(phone);
        Map<String,Object> map=new HashMap<>();
        if(buyer!=null){
            map.put("msg","ok");
        }else{
            map.put("msg","fail");
        }
        return map;
    }

    @RequestMapping("/seleByPhoneup")
    @ResponseBody
    public Map<String,Object> seleByPhoneup(String phone,String password){
        Buyer buyer = buyerService.seleByPhone(phone);
        String encode = bCryptPasswordEncoder.encode(password);
        buyer.setPassword(encode);
        Integer integer = buyerService.upBuyer(buyer);
        System.out.println("看看结果"+integer);
        Map<String,Object> map=new HashMap<>();
        if(integer>0){
            map.put("msg","success");
        }else{
            map.put("msg","fail");
        }
        return map;
    }

    @Resource
    CouponService couponService;
   //查询出挡墙登陆的对象
    @RequestMapping("/seleBuyer")
    @ResponseBody
    public Map<String,Object> seleBuyer(){
        String phone = CrowdBuyerDetailService.phone;
        Buyer buyer = buyerService.seleByPhone(phone);
        //查询出此用户的优惠卷个数
        Integer coupon = couponService.seleCouByUserid(buyer.getUserid());
        Map<String,Object> map=new HashMap<>();
        map.put("buyer",buyer);
        map.put("coupon",coupon);
        return map;
    }

    String imgURL;
    @RequestMapping("/uptouxiang")
    @ResponseBody
    public Map<String,Object> uptouxiang(@RequestParam("avatar_file")MultipartFile multipartFile) throws IOException {
        InputStream inputStream=multipartFile.getInputStream();
        String oldFileName = multipartFile.getOriginalFilename();
         imgURL = Aliyun.upload(inputStream, oldFileName);
        Map<String,Object> map=new HashMap<>();
        map.put("result",imgURL);
    return map;
    }
    //修改用户信息
    @RequestMapping("/upBuyer")
    @ResponseBody
    public Map<String,Object> upBuyer(@RequestBody Buyer buyer) throws IOException {
        if(!"".equals(imgURL)){
            buyer.setHeadimage(imgURL);
        }
        Integer integer = buyerService.upBuyer(buyer);

        Map<String,Object> map=new HashMap<>();
        if(integer>0){
            map.put("msg","SUCCESS");
        }else{
            map.put("msg","FAIL");
        }
        return map;
    }

    //根据phone查询用户
    @GetMapping("/buyer/query/by/phone")
    @ResponseBody
    public Buyer buyerByphone(){
        Buyer buyer=buyerService.seleByPhone(CrowdBuyerDetailService.phone);
        return buyer;
    }
}
